package com.excilys.formation.project.integration;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class VirementTestIntegration {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();

    @Before
    public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "http://192.168.10.93:8180/project_bank_webapp-0.0.1-SNAPSHOT";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get(baseUrl + "/login");

        new WebDriverWait(driver, 2).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(final WebDriver d) {
                return d.getCurrentUrl().startsWith(baseUrl + "/login");
            }
        });

        driver.findElement(By.name("j_username")).clear();
        driver.findElement(By.name("j_username")).sendKeys("vpeignot");
        driver.findElement(By.name("j_password")).clear();
        driver.findElement(By.name("j_password")).sendKeys("vpeignot");
        driver.findElement(By.name("login")).click();
    }

    @Test
    public void testVirement() throws Exception {
        driver.findElement(By.linkText("Nouveau virement")).click();
        driver.findElement(By.id("recepteur")).clear();
        driver.findElement(By.id("recepteur")).sendKeys("34567890123456789012345");
        driver.findElement(By.id("montant")).clear();
        driver.findElement(By.id("montant")).sendKeys("400");
        driver.findElement(By.cssSelector("button[type=\"submit\"]")).click();
        new WebDriverWait(driver, 20).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(final WebDriver d) {
                return d.getCurrentUrl().startsWith(baseUrl + "/secure/virement/listvirement");
            }
        });
        assertEquals(baseUrl + "/secure/virement/listvirement", driver.getCurrentUrl());
    }

    @Test
    public void testVirementToSameAccount() throws Exception {
        driver.findElement(By.linkText("Nouveau virement")).click();
        new Select(driver.findElement(By.id("emetteur"))).selectByIndex(1);
        driver.findElement(By.id("recepteur")).clear();
        driver.findElement(By.id("recepteur")).sendKeys("45678901234567890123456");
        driver.findElement(By.id("montant")).clear();
        driver.findElement(By.id("montant")).sendKeys("400");
        driver.findElement(By.cssSelector("button[type=\"submit\"]")).click();

        new WebDriverWait(driver, 2).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(final WebDriver d) {
                return d.getCurrentUrl().startsWith(baseUrl + "/secure/virement");
            }
        });

        assertEquals(baseUrl + "/secure/virement", driver.getCurrentUrl());
    }

    @Test
    public void testVirementFail() throws Exception {
        driver.findElement(By.linkText("Nouveau virement")).click();
        driver.findElement(By.id("recepteur")).clear();
        driver.findElement(By.id("recepteur")).sendKeys("45678901234567890123456");
        driver.findElement(By.id("montant")).clear();
        driver.findElement(By.id("montant")).sendKeys("-400");
        driver.findElement(By.cssSelector("button[type=\"submit\"]")).click();

        // FIXME succès automatique...
        new WebDriverWait(driver, 2).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(final WebDriver d) {
                return d.getCurrentUrl().startsWith(baseUrl + "/secure/virement");
            }
        });

        assertEquals(baseUrl + "/secure/virement", driver.getCurrentUrl());
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }
}
